package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Composition;
import com.ultrasoft.farmasoft.domain.Productattributes;


@Repository(value = "productAttributesDao")
@Transactional(readOnly = false)
public class ProductAttributesDaoImpl implements ProductAttributesDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	public List<Productattributes> getListProductAttributes() {
		TypedQuery<Productattributes> q = em.createNamedQuery("Productattributes.findAll",Productattributes.class);
    	return q.getResultList();
	}

	public List<Productattributes> getListProductAttributesByProviderId(Integer providerId) {
		TypedQuery<Productattributes> q = em.createNamedQuery("Productattributes.findByProviderId",Productattributes.class);
    	q.setParameter("providerId",providerId);
    	return q.getResultList();
	}

	public List<Productattributes> getListProductAttributesByProductId(Integer productId) {
		TypedQuery<Productattributes> q = em.createNamedQuery("Productattributes.findByProductId",Productattributes.class);
    	q.setParameter("productId",productId);
    	return q.getResultList();
	}

	public List<Productattributes> getListProductAttributesByProductAttributeId(Integer productAttributeId) {
		TypedQuery<Productattributes> q = em.createNamedQuery("Productattributes.findByProductAttributeId",Productattributes.class);
    	q.setParameter("productAttributeId",productAttributeId);
    	return q.getResultList();
	}

	public List<Productattributes> getListProductAttributesByProviderIdByProductId(Integer providerId,Integer productId) {
		TypedQuery<Productattributes> q = em.createNamedQuery("Productattributes.findByProductIdByProviderId",Productattributes.class);
    	q.setParameter("providerId",providerId);
    	q.setParameter("productId",productId);
    	return q.getResultList();
	}
	
	public Productattributes findByProviderIdByProductIdByProductAttributeId(Integer providerId, Integer productId, Integer productAttributeId) {
		Productattributes res = null;
    	Query  q = em.createNamedQuery("Productattributes.findByProductIdByProviderIdByProductAttributeId");
    	q.setParameter("providerId",providerId);
    	q.setParameter("productId",productId);
    	q.setParameter("productAttributeId",productAttributeId);
    	if(q.getResultList().size()==1)
    		res=(Productattributes)q.getSingleResult();
    	return res;
	}

	public void remove(Integer providerId,Integer productId) {
		Query q = em.createNamedQuery("Productattributes.deleteByProviderIdByProductId");
		q.setParameter("providerId",providerId);
		q.setParameter("productId",productId);
		q.executeUpdate();
	}
	
	public void update(Productattributes productAttributes) {
		em.merge(productAttributes);
	}

	public void add(Productattributes productAttributes) {
		em.persist(productAttributes);
	}
}
